#!/bin/bash

function cmdReturnCheck()
{
    EXEC=$1
    echo "execute command: '$EXEC'."
    $EXEC
    if [ $? -ne 0 ]; then
        echo "execute command: '$EXEC' fail."
        exit 1
    else
        echo "execute command: '$EXEC' success."
    fi
}

function cmdSun20iDependent()
{
    echo "check dependent."

    if [ ! -f /usr/local/bin/xfel ]; then
        cmdReturnCheck "make -C tools/xfel"
        cmdReturnCheck "sudo cp tools/xfel/xfel /usr/local/bin/xfel"
    fi

    if [ ! -f /etc/udev/rules.d/99-xfel.rules ]; then
        cmdReturnCheck "sudo cp tools/xfel/99-xfel.rules /etc/udev/rules.d/99-xfel.rules"
        cmdReturnCheck "sudo systemctl restart udev"
    fi

    if [ ! -d tardl ]; then
        cmdReturnCheck "mkdir tardl"
    fi

    if [ ! -f tardl/buildroot-2024.08.tar.xz ]; then
        cmdReturnCheck "wget -c https://buildroot.org/downloads/buildroot-2024.08.tar.xz -O tardl/buildroot-2024.08.tar.xz"
    fi

    if [ ! -d source/buildroot/buildroot-2024.08 ]; then
        pushd source/buildroot
            cmdReturnCheck "tar -xvf ../../tardl/buildroot-2024.08.tar.xz"
        popd
    fi

    if [ ! -f source/buildroot/buildroot-2024.08/configs/baba-fly-rtl8188eu_defconfig ]; then
        cmdReturnCheck "cp $PWD/source/buildroot/allwinner/configs/baba-fly-rtl8188eu_defconfig source/buildroot/buildroot-2024.08/configs/baba-fly-rtl8188eu_defconfig"
    fi

    if [ ! -d source/buildroot/buildroot-2024.08/board/allwinner-t113s3 ]; then
        cmdReturnCheck "ln -s $PWD/source/buildroot/allwinner/board/allwinner-t113s3 $PWD/source/buildroot/buildroot-2024.08/board/allwinner-t113s3"
    fi

    if [ ! -f source/buildroot/buildroot-2024.08/.config ]; then
        pushd source/buildroot/buildroot-2024.08
            cmdReturnCheck "make baba-fly-rtl8188eu_defconfig"
        popd
    fi
}

function cmdSun20iBuildRoot()
{
    cmdSun20iDependent
    pushd source/buildroot/buildroot-2024.08
        cmdReturnCheck "make"
    popd
}

function cmdSun20iBuildBoot()
{
    cmdReturnCheck "make -C source/buildroot/allwinner/board/allwinner-t113s3/awboot spi-boot.img"
}

function cmdSun20iBuildAll()
{
    cmdSun20iBuildRoot
    if [ ! -f source/buildroot/allwinner/board/allwinner-t113s3/awboot/spi-boot.img ]; then
        cmdSun20iBuildBoot
    fi
}

function cmdSun20iBuildRootMenuConfig()
{
    cmdSun20iDependent
    pushd source/buildroot/buildroot-2024.08
        cmdReturnCheck "make menuconfig"
    popd
}

function cmdSun20iBuildRootSaveConfig()
{
    cmdSun20iDependent
    pushd source/buildroot/buildroot-2024.08
        cmdReturnCheck "make savedefconfig"
    popd
    cmdReturnCheck "cp source/buildroot/buildroot-2024.08/configs/baba-fly-rtl8188eu_defconfig source/buildroot/allwinner/configs/baba-fly-rtl8188eu_defconfig"
}

function cmdSun20iLinuxBuild()
{
    cmdSun20iDependent
    pushd source/buildroot/buildroot-2024.08
        cmdReturnCheck "make linux-rebuild"
    popd
}

function cmdSun20iLinuxMenuConfig()
{
    cmdSun20iDependent
    pushd source/buildroot/buildroot-2024.08
        cmdReturnCheck "make linux-menuconfig"
    popd
}

function cmdSun20iLinuxSaveConfig()
{
    cmdSun20iDependent
    pushd source/buildroot/buildroot-2024.08
        cmdReturnCheck "make linux-savedefconfig"
    popd
    cmdReturnCheck "cp source/buildroot/buildroot-2024.08/output/build/linux-6.10.8/defconfig source/buildroot/allwinner/board/allwinner-t113s3/linux/baba-fly-rtl8188eu_defconfig"
}

function cmdSun20iFlash()
{
    if [ ! -f source/buildroot/allwinner/board/allwinner-t113s3/awboot/spi-boot.img ]; then
        echo "source/buildroot/allwinner/board/allwinner-t113s3/awboot/spi-boot.img no such file"
        exit 1
    fi

    if [ ! -f source/buildroot/buildroot-2024.08/output/images/baba-fly.dtb ]; then
        echo "source/buildroot/buildroot-2024.08/output/images/baba-fly.dtb no such file"
        exit 1
    fi

    if [ ! -f source/buildroot/buildroot-2024.08/output/images/zImage ]; then
        echo "source/buildroot/buildroot-2024.08/output/images/zImage no such file"
        exit 1
    fi

    if [ ! -f source/buildroot/buildroot-2024.08/output/images/rootfs.ubifs ]; then
        echo "source/buildroot/buildroot-2024.08/output/images/rootfs.ubifs no such file"
        exit 1
    fi

    cmdReturnCheck "xfel spinand erase 0x000000 0x8000000"
    cmdReturnCheck "xfel spinand write 0x000000 source/buildroot/allwinner/board/allwinner-t113s3/awboot/spi-boot.img"
    cmdReturnCheck "xfel spinand write 0x040000 source/buildroot/buildroot-2024.08/output/images/baba-fly.dtb"
    cmdReturnCheck "xfel spinand write 0x080000 source/buildroot/buildroot-2024.08/output/images/zImage"
    cmdReturnCheck "xfel spinand write 0x800000 source/buildroot/buildroot-2024.08/output/images/rootfs.ubi"
}

function cmdSun20iFlashDts()
{
    if [ ! -f source/buildroot/buildroot-2024.08/output/images/baba-fly.dtb ]; then
        echo "source/buildroot/buildroot-2024.08/output/images/baba-fly.dtb no such file"
        exit 1
    fi

    cmdReturnCheck "xfel spinand write 0x040000 source/buildroot/buildroot-2024.08/output/images/baba-fly.dtb"
}

function cmdCpuHelp()
{
    echo "help                      : display help."
    echo "all                       : build all (default)."
    echo "buildroot-build           : buildroot build."
    echo "buildroot-menuconfig      : buildroot menuconfig."
    echo "buildroot-savedefconfig   : buildroot save defconfig."
    echo "linux-build               : linux build."
    echo "linux-menuconfig          : linux menuconfig."
    echo "linux-savedefconfig       : linux save defconfig."
    echo "boot-build                : awboot build."
    echo "flash                     : flash images."
    echo "flash-dts                 : flash dts."
    echo "clean                     : clean source"
}

function cmdCpuClean()
{
    cmdReturnCheck "make -C source/buildroot/allwinner/board/allwinner-t113s3/awboot clean"
}

function cmdCpu()
{
    case $1 in
        "help")
            cmdCpuHelp
        ;;

        "all")
            cmdSun20iBuildAll
        ;;

        "buildroot-build")
            cmdSun20iBuildRoot
        ;;

        "buildroot-menuconfig")
            cmdSun20iBuildRootMenuConfig
        ;;

        "buildroot-savedefconfig")
            cmdSun20iBuildRootSaveConfig
        ;;

        "linux-build")
            cmdSun20iLinuxBuild
        ;;

        "linux-menuconfig")
            cmdSun20iLinuxMenuConfig
        ;;

        "linux-savedefconfig")
            cmdSun20iLinuxSaveConfig
        ;;

        "boot-build")
            cmdSun20iBuildBoot
        ;;

        "flash")
            cmdSun20iFlash
        ;;

        "flash-dts")
            cmdSun20iFlashDts
        ;;

        "clean")
            cmdCpuClean
        ;;

        *)
            cmdCpuHelp
        ;;
    esac
}
